package com.zsk.shop.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zsk.shop.common.base.BaseResponse;
import com.zsk.shop.common.base.page.BasePageFactory;
import com.zsk.shop.common.utils.HttpContext;
import com.zsk.shop.common.utils.RegexCheckUtils;
import com.zsk.shop.common.utils.ToolUtil;
import com.zsk.shop.entity.AliPayAccountUser;
import com.zsk.shop.entity.ShopUser;
import com.zsk.shop.mapper.AliPayAccountUserMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @author Ys
 * @date 2021年12月10日 11:11
 */
@Service
public class AliPayAccountUserService extends ServiceImpl<AliPayAccountUserMapper, AliPayAccountUser> {

    private static final Logger logger = LoggerFactory.getLogger(AliPayAccountUserService.class);

    @Resource
    private ShopUserService userService;

    public BaseResponse add(String accountName) {
        //获取用户实名名称
        String userNo = HttpContext.getUserNo();
        ShopUser user = userService.queryByUserNo(userNo);
        if (ToolUtil.isNotEmpty(user)) {
            if (ToolUtil.isEmpty(user.getTrueName()) || ToolUtil.isEmpty(user.getIdentityCard())) {
                return BaseResponse.error("当前账号未实名，请完成实名操作");
            }
        }
        //添加支付宝账户数据
        if (ToolUtil.isNotEmpty(getByUser(userNo))) {
            return BaseResponse.error("该账号已设置过支付宝账号");
        }
        //校验支付宝账号的真实性(暂时不处理)
        //定义是否全校验不通过
        Boolean f;
        //判断是否为手机号或者邮箱
        if (RegexCheckUtils.isNotEmail(accountName)) {
            if (RegexCheckUtils.isNotMobile(accountName)) {
                f = true;
            } else {
                f = false;
            }
        } else {
            f = false;
        }
        if (f) {
            return BaseResponse.error("请检查该支付宝账号是否符合（手机号/邮箱）规则");
        }
        AliPayAccountUser accountUser = AliPayAccountUser.builder().userNo(userNo).alipayAccount(accountName).createTime(new Date()).isDeleted(false).build();
        this.save(accountUser);
        return BaseResponse.success("添加个人用户支付宝账号成功");
    }

    public AliPayAccountUser getByUser(String userNo) {
        LambdaQueryWrapper<AliPayAccountUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AliPayAccountUser::getUserNo, userNo);
        queryWrapper.eq(AliPayAccountUser::getIsDeleted, false);
        queryWrapper.last("limit 1");
        return getOne(queryWrapper);
    }

    public BaseResponse accountList() {
        Page pageContext = BasePageFactory.defaultPage();
        LambdaQueryWrapper<AliPayAccountUser> queryWrapper = new LambdaQueryWrapper<>();
        IPage page = this.page(pageContext, queryWrapper);
        return BasePageFactory.createPageInfo(page);
    }

    public BaseResponse editUserAliPayAccount(String id, String accountName) {
        LambdaUpdateWrapper<AliPayAccountUser> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(AliPayAccountUser::getAlipayAccount, accountName);
        updateWrapper.eq(AliPayAccountUser::getId, id);
        this.update(updateWrapper);
        return BaseResponse.success("修改用户账户成功");
    }

    public BaseResponse info() {
        String userNo = HttpContext.getUserNo();
        return BaseResponse.success(getByUser(userNo));
    }

    public BaseResponse delete(Long id) {
        AliPayAccountUser alipayAccountUser = this.getById(id);
        if (ToolUtil.isEmpty(id)) {
            return BaseResponse.error("当前数据不存在");
        }
        alipayAccountUser.setIsDeleted(true);
        alipayAccountUser.setUpdateTime(new Date());
        this.updateById(alipayAccountUser);
        return BaseResponse.success("删除支付宝绑定数据成功");
    }
}
